001    import javax.swing.*;
002    /**
003     * This is a Swing graphical user interface to the hangman game.
004     * It displays the game board using elementary Swing widgets and 
005     * allows the player to control the game via buttons.
006     * Refer to the user interface prototype.
007     */
008    public class SwingUI extends javax.swing.JFrame implements HangmanUI
009    {
010    
011        /** A text area for displaying the guessed letters */
012        private javax.swing.JTextArea txtBoard;
013        /** The title of the game */
014        private javax.swing.JLabel lblTitle;
015        /** A panel to contain the row of buttons */
016        private javax.swing.JPanel buttonPanel;
017        /** A label for the turn counter */
018        private javax.swing.JLabel lblTurns;
019    
020        /** array of buttons for the letters of the alphabet */
021        private javax.swing.JButton[] LetterButtons;
022    
023        /** the reference to the hangman Board */
024        Board board;
025    
026        /** the reference to the parent, the HangmanLogic */
027        private HangmanLogic parent;
028    
029        /** Creates new frame to contain all the user interface components
030          *  for the game.
031          */
032        public SwingUI()
033        {
034            initComponents ();
035            makeButtons();
036            pack ();
037        }
038    
039        /**
040         *  Make the interface visible
041         */
042        public void display()
043        {
044        }
045    
046        /**
047         *  Save a reference to the instance of the Board
048         *  and then show the board.
049         */
050        public void setBoard(Board theBoard)
051        {
052        }
053    
054        /**
055         *  Save a reference to the parent (an instance of HangmanLogic)
056         */
057        public void setParent(HangmanLogic theParent)
058        {
059        }
060    
061    
062        /**
063         *   Display the current state of the board.
064         *   Specifically, display the turn counter and
065         *   display the board in some formatted manner.
066         */
067        public void showBoard()
068        {
069            // Show the turn counter
070            // Get the formatted board and show it
071        }
072    
073        /**
074         *   Display that the player won the game.
075         */
076        public void showWin()
077        {
078        }
079    
080        /**
081         *   Display that the player lost the game.
082         *   Reveal the solution.
083         */
084        public void showLose()
085        {
086        }
087    
088        /** This method is called from within the constructor to
089         * initialize the form.
090         */
091        private void initComponents()
092        {
093            // title bar
094    
095            // Labels
096    
097            // display of the game board
098    
099            // Button panel to hold the buttons
100    
101            // window listener to handle window closing event
102            addWindowListener(new java.awt.event.WindowAdapter()
103                              {
104                                  public void windowClosing(java.awt.event.WindowEvent evt)
105                                  {
106                                      exitForm(evt);
107                                  }
108                              }
109                             );
110    
111        }
112    
113        /** 
114         *  Create the 26 letter buttons, one for each letter of
115         *  the alphabet.
116         */
117        private void makeButtons()
118        {
119            // declare an array of 26 buttons
120    
121            // for each button
122                // Create a button with its label as the corresponding letter
123                // put the button in the array
124                // set button size and margins
125                // add button listener
126                // add the button to the panel                                              
127        }
128    
129    
130        /** 
131         *  Handle a button press by the user.
132         *  Determine which letter was chosen.
133         */
134        private void LetterButtonActionPerformed(java.awt.event.ActionEvent evt)
135        {
136            // Iterate through all buttons until we find the one that
137            // was the source of the button action even.
138            // Disable the button (grey out)
139            // Call processMove, sending it the letter that was chosen.
140        }
141    
142    
143        /** 
144         *  Handle end of game, asking if the player wants another game
145         *  If yes, start a new game, otherwise exit.
146         */
147        public void playAgain()
148        {
149            // show confirm dialog, which returns 0 for yes, 1 for no.
150    
151            // Call new game routine
152            // Reset (enable) all the letter buttons
153            // if user said no then terminate.
154        }
155    
156        /** 
157         *  Handle window exit: Terminate the application 
158         */
159        private void exitForm(java.awt.event.WindowEvent evt)
160        {
161        }
162    
163    }